Google Cloud Platform
GAE (Google App Engine)
Google App Engine
is a cloud computing platform for developing and hosting web applications in Google-managed data centers.
GCE (Google Compute Engine)
Google Compute Engine
delivers virtual machines running in Google’s innovative data centers and worldwide fiber network.
GKE (Google Container Engine)
https://cloud.google.com/containers/
Dataflow
Dataflow
is a unified programming model and a managed service for developing and executing a wide range of data processing patterns including ETL, batch computation, and continuous computation.
BigTable
Cloud Bigtable
is Google’s NoSQL Big Data database service. It’s the same database that powers many core Google services, including Search, Analytics, Maps, and Gmail.
BigQuery
BigQuery
is Google’s fully managed, petabyte scale, low cost enterprise data warehouse for analytics.
Others
- PubSub
- Datastore
- Storage
- Monitoring
What is a typical BigData project (web+analysis)
- Service (GAE/GKE) to receive user generated contents
- Datastore (BigTable/Spanner) to save contents
- Index (ElasticSearch) to query contents in runtime
- Service (GAE/GKE) to serve contents in runtime
- Dataflow (MapReduce) to dump data into BigQuery
- BigQuery (SQL) to perform further analysis
GKE is more like a simple GCE
Google App Engine Flex
App Engine flexible environment
automatically scales your app up and down while balancing the load. Microservices, authorization, SQL and NoSQL databases, traffic splitting, logging.
Step 3.1.1 In your Intellij, create a new file called app.yaml
The content has just two lines.
GAE 通过flex实现负载均衡
1 | runtime: go |
Recommended: upload your two files (main.go and app.yaml) into github.
Worst cast: just copy and paste in terminals.
More readings
- Overview https://cloud.google.com/docs/overview/
- Flex in GAE https://cloud.google.com/appengine/docs/flexible/
- GAE on Golang https://cloud.google.com/appengine/docs/go/
- GAE in go: https://cloud.google.com/appengine/docs/flexible/go/quickstart
Setup cloud terminal.
Question: Why use cloud terminal?
Answer: Such that we do not rely on the local environment (Windows vs. Mac).
Click ‘Activate Google Cloud Shell’. Wait until the init is done
Then you will see a vm available for you to play. This is similar to the EC2 vm.
Install go libraries in gcloud terminal.
1 | go get gopkg.in/olivere/elastic.v3 |
(Recommended) Checkout your codes from github
git clone https://github.com/Luorinz/Around.git
Question: what’s the purpose of app.yaml?
Answer: to tell google that your program is a go program and use go compiler to run it.
Then enter this line of code to make sure you copy past is done.
1 | cat main.go | head -n 5 |
You’re expected to see this1
2
3
4
5package main
import (
elastic "gopkg.in/olivere/elastic.v3"
"fmt"
Enter gcloud app deploy
When asked about site, enter 1, which is us-west2
When asked Y/n, enter Y. The deployment may take more than 10 minutes to finish (next deployment will be faster).
Error: If you have error like this, you may have a wrong ES_URL. Update the value and deploy again.
1 | Updating service [default]...failed. |
Open Postman, find the post query from history.
(POST, url=http://xxx.appspot.com/post)
Replace xxx with your real project id
1 | { |
And then find the get query from history. (GET, url=http://around-xxxx.appspot.com/search?lat=37.5&lon=-120)
You should get the results back
1 | [ |
Play with more examples. If you do not have any results back, try to change your lat or lon to make sure it’s within 200km.
Check the logging to see if there is any error message
Choose SATCKDRIVER -> Logging -> Logs
(Optional) Stop Instance to save credit.
(Optional) Start the instance again. In the same page, choose one instance and click START. If the START is disabled, refresh it or wait a minute.
(Optional) How to filter word
1 | func containsFilteredWords(s *string) bool { |
Update relevant part from your code
1 | func handlerSearch(w http.ResponseWriter, r *http.Request) { |
Related string operationshttps://golang.org/pkg/strings/
(Optional) Useful Go information
Error vs Panic
- Error: normal way to handle error status.
Panic: normally used when some “impossible” situation happens with no intend recover.
- Use “recover” to handle when exiting function.
- recover is like catch
“defer” clause
- Preferred way to recycle resources
- E.g. file handles, network connections, etc.
- Defer executes after return
- First come last executes
- Example
1 | func Foo() { |
- Combined with recover to handle panic
1 | func Foo() result string, err error { |
- Go’s multiprocessing facility: Communication Sequential Processes
- https://en.wikipedia.org/wiki/Communicating_sequential_processes
- Lightweight concurrency, synchronize using message passing
- Go routine
- Go’s concurrency mechanism
- Lightweight (corresponding to fiber)
- Growable stacks
- Scheduling
- m:n scheduling - m goroutine on n OS threads
- Scheduler invoked implicitly by language construct
- E.g. time.Sleep, I/O, etc.
- Have no identity - nothing like thread id
- Avoid thread-local storage (TLS, tend to be misused, similar to global variable)
- Easy go routine migration
- Example
1 | // clock.go |
1 | // netcat.go |
- Go channel
- Communication mechanism for Go routine
- Example
1 | // channel_example.go |
- More reading
- https://golang.org/doc/
- The Go Programming Language - http://www.gopl.io/
- https://blog.golang.org/